<?php
/**
 * robert added
 * webform send email logic
 */
function webformview_send_email_RLM($title, $content, $submission_output) {
  $email_template01 = variable_get('BB_ST_email_template01');
  $email['subject'] = $title;
  $email['address'] = array();
  $email['addresscc'] = array();

  //-------------------defined submission variable---------------------------
  $initiator_email = $content['initiator_email'];
  $receiving_line_manager_email = $content['receiving_line_manager_email'];
  $approver_email = $content['approver_email'];
  $receiving_hr_manager_email = $content['receiving_hr_manager_email'];
  $email_crown_arr = $content['email_crown_arr'];
  $email_contact_others = $content['email_contact_others'];
  //$relocation_initiator_role = $content['relocation_initiator_role'];
  $employee_last_name = $content['employee_last_name'];
  $employee_first_name = $content['employee_first_name'];
  $receiving_cost_centre = $content['receiving_cost_centre'];
  $relocation_effective_date = $content['relocation_effective_date'];
  if(isset($content['sending_work_location_site']) && trim($content['sending_work_location_site']) == '' ){
    if(isset($content['sending_work_city']) && $content['sending_work_city'] == LOCATION_CITY_OTHERS ){
      $sending_work_location_site = $content['please_specify_sending'];
    }else{
      $sending_work_location_site = $content['sending_work_city'];
    }
  }else{
    $sending_work_location_site = $content['sending_work_location_site'];
  }
  if(isset($content['receiving_work_location_site']) && trim($content['receiving_work_location_site']) == '' ){
    if(isset($content['receiving_work_city']) && $content['receiving_work_city'] == LOCATION_CITY_OTHERS ){
      $receiving_work_location_site = $content['please_specify_receiving'];
    }else{
      $receiving_work_location_site = $content['receiving_work_city'];
    }
  }else{
    $receiving_work_location_site = $content['receiving_work_location_site'];
  }
  $initiator_last_name = $content['initiator_last_name'];
  $initiator_first_name = $content['initiator_first_name'];
  //---------------------- end ------------------------------
  
  

  //----------- email----------------
  if ($initiator_email) {
    $email['address'][] = $initiator_email;
  }
  if ($receiving_line_manager_email) {
    $email['address'][] = $receiving_line_manager_email;
  }
  if ($approver_email) {
    $email['address'][] = $approver_email;
  }
  if ($receiving_hr_manager_email) {
    $email['addresscc'][] = $receiving_hr_manager_email;
  }
  if (is_array($email_crown_arr)) {
    foreach ($email_crown_arr as $item) {
      if ($item) {
        $email['address'][] = trim($item);
      }
    }
  }
  //----------- end email----------------

  $email_template01 = str_replace("<employee_last_name>", $employee_last_name, $email_template01);
  $email_template01 = str_replace("<employee_first_name>", $employee_first_name, $email_template01);
  $email_template01 = str_replace("<sending_work_location_site>", $sending_work_location_site, $email_template01);
  $email_template01 = str_replace("<receiving_work_location_site>", $receiving_work_location_site, $email_template01);
  $email_template01 = str_replace("<effective_date>", $relocation_effective_date, $email_template01);
  $email_template01 = str_replace("<receiving_cost_centre>", $receiving_cost_centre, $email_template01);
  $email_template01 = str_replace("<contact_information>", $email_contact_others, $email_template01);
  
  $email_template01 .= "\r\n" . $submission_output;
  
  //print_r($email);die;
  $email['subject'] = 'Confirmation - Domestic Relocation Initiation for ' . $employee_last_name . ', ' . $employee_first_name . ' submitted';
  $rs = postmail_webformview($email['address'], $email['addresscc'], $email['subject'], $email_template01);
}

function webformview_send_email_NOTRLM($title, $content, $submission_output){
  $email_template02 = variable_get('BB_ST_email_template02');
  $email['subject'] = $title;
  $email['address'] = array();
  $email['addresscc'] = array();

  //-------------------defined submission variable---------------------------
  $initiator_email = $content['initiator_email'];
  $receiving_line_manager_email = $content['receiving_line_manager_email'];
  $approver_email = $content['approver_email'];
  $receiving_hr_manager_email = $content['receiving_hr_manager_email'];
  $email_crown_arr = $content['email_crown_arr'];
  $email_contact_others = $content['email_contact_others'];
  //$relocation_initiator_role = $content['relocation_initiator_role'];
  $employee_last_name = $content['employee_last_name'];
  $employee_first_name = $content['employee_first_name'];
  $receiving_cost_centre = $content['receiving_cost_centre'];
  $relocation_effective_date = $content['relocation_effective_date'];
  if(isset($content['sending_work_location_site']) && trim($content['sending_work_location_site']) == '' ){
    if(isset($content['sending_work_city']) && $content['sending_work_city'] == LOCATION_CITY_OTHERS ){
      $sending_work_location_site = $content['please_specify_sending'];
    }else{
      $sending_work_location_site = $content['sending_work_city'];
    }
  }else{
    $sending_work_location_site = $content['sending_work_location_site'];
  }
  if(isset($content['receiving_work_location_site']) && trim($content['receiving_work_location_site']) == '' ){
    if(isset($content['receiving_work_city']) && $content['receiving_work_city'] == LOCATION_CITY_OTHERS ){
      $receiving_work_location_site = $content['please_specify_receiving'];
    }else{
      $receiving_work_location_site = $content['receiving_work_city'];
    }
  }else{
    $receiving_work_location_site = $content['receiving_work_location_site'];
  }
  $initiator_last_name = $content['initiator_last_name'];
  $initiator_first_name = $content['initiator_first_name'];
  //---------------------- end ------------------------------
  
  
  
  
  //-----------email----------------
  if ($initiator_email) {
    $email['addresscc'][] = $initiator_email;
  }
  if ($receiving_line_manager_email) {
    $email['address'][] = $receiving_line_manager_email;
  }
  if ($approver_email) {
    $email['addresscc'][] = $approver_email;
  }
  if ($receiving_hr_manager_email) {
    $email['addresscc'][] = $receiving_hr_manager_email;
  }
  if (is_array($email_crown_arr)) {
    foreach ($email_crown_arr as $item) {
      if ($item) {
        $email['addresscc'][] = trim($item);
      }
    }
  }
  //----------- end email----------------

  $email_template02 = str_replace("<submitter_name>", $initiator_last_name . ', ' . $initiator_first_name, $email_template02);
  $email_template02 = str_replace("<employee_last_name>", $employee_last_name, $email_template02);
  $email_template02 = str_replace("<employee_first_name>", $employee_first_name, $email_template02);
  $email_template02 = str_replace("<sending_work_location_site>", $sending_work_location_site, $email_template02);
  $email_template02 = str_replace("<receiving_work_location_site>", $receiving_work_location_site, $email_template02);
  $email_template02 = str_replace("<effective_date>", $relocation_effective_date, $email_template02);
  $email_template02 = str_replace("<receiving_cost_centre>", $receiving_cost_centre, $email_template02);
  $email_template02 = str_replace("<contact_information>", $email_contact_others, $email_template02);
  $email_template02 .= "\r\n" . $submission_output;

  //print_r($email);die;
  $email['subject'] = 'PLEASE READ: Action may be required -  Domestic Relocation Initiation for ' . $employee_last_name . ', ' . $employee_first_name . ' submitted';
  $rs = postmail_webformview($email['address'], $email['addresscc'], $email['subject'], $email_template02);
}


function webformview_send_email_cancellation($title, $content, $submission_output) {
  $email['address'] = array();
  $email['addresscc'] = array();

  //-------------------defined submission variable---------------------------
  $initiator_email = $content['initiator_email'];
  $receiving_line_manager_email = $content['receiving_line_manager_email'];
  $approver_email = $content['approver_email'];
  $receiving_hr_manager_email = $content['receiving_hr_manager_email'];
  $email_crown_arr = $content['email_crown_arr'];
  $email_contact_others = $content['email_contact_others'];
  
  $country_of_domestic_transfer = $content['country_of_domestic_transfer'];
  $global_sap_id = $content['global_sap_id'];
  $relocation_effective_date = $content['relocation_effective_date'];

  $cancellation_reason = $content['cancellation_reason'];
  //$cancellation_time = $content['cancellation_time'];
  $cancel_user_email = $content['cancel_user_email'];
  
  $employee_last_name = $content['employee_last_name'];
  $employee_first_name = $content['employee_first_name'];
  if(isset($content['sending_work_location_site']) && trim($content['sending_work_location_site']) == '' ){
    if(isset($content['sending_work_city']) && $content['sending_work_city'] == LOCATION_CITY_OTHERS ){
      $sending_work_location_site = $content['please_specify_sending'];
    }else{
      $sending_work_location_site = $content['sending_work_city'];
    }
  }else{
    $sending_work_location_site = $content['sending_work_location_site'];
  }
  if(isset($content['receiving_work_location_site']) && trim($content['receiving_work_location_site']) == '' ){
    if(isset($content['receiving_work_city']) && $content['receiving_work_city'] == LOCATION_CITY_OTHERS ){
      $receiving_work_location_site = $content['please_specify_receiving'];
    }else{
      $receiving_work_location_site = $content['receiving_work_city'];
    }
  }else{
    $receiving_work_location_site = $content['receiving_work_location_site'];
  }
  //---------------------- end ------------------------------
  //----------- email----------------
  if ($initiator_email) {
    $email['address'][] = $initiator_email;
  }
  if ($receiving_line_manager_email) {
    $email['address'][] = $receiving_line_manager_email;
  }
  if ($approver_email) {
    $email['address'][] = $approver_email;
  }
  if ($receiving_hr_manager_email) {
    $email['addresscc'][] = $receiving_hr_manager_email;
  }
  if (is_array($email_crown_arr)) {
    foreach ($email_crown_arr as $item) {
      if ($item) {
        $email['address'][] = trim($item);
      }
    }
  }

  $email_contact_others_arr = explode(',', $email_contact_others);
  if (is_array($email_contact_others_arr)) {
    foreach ($email_contact_others_arr as $item) {
      if ($item) {
        $email['address'][] = trim($item);
      }
    }
  }
  //----------- end email----------------

  $email_template = variable_get('BB_ST_EMAIL_TEMPLATE_CANCELLATION');

  $email_template = str_replace("<employee_name>", $employee_last_name . ', ' . $employee_first_name, $email_template);
  $email_template = str_replace("<sending_work_location_site>", $sending_work_location_site, $email_template);
  $email_template = str_replace("<receiving_work_location_site>", $receiving_work_location_site, $email_template);
  $email_template = str_replace("<contact_information>", $email_contact_others, $email_template);
  
  $email_template = str_replace("<country>", $country_of_domestic_transfer, $email_template);
  $email_template = str_replace("<sap_id>", $global_sap_id, $email_template);
  $email_template = str_replace("<effective_date>", $relocation_effective_date, $email_template);
  
  $email_template = str_replace("<cancellation_reason>", $cancellation_reason, $email_template);
  $email_template = str_replace("<cancel_user_email>", $cancel_user_email, $email_template);
  
  $email['subject'] = 'Cancellation - Domestic Relocation Initiation for '.$employee_last_name . ', ' . $employee_first_name.' submitted';
  $rs = postmail_webformview($email['address'], $email['addresscc'], $email['subject'], $email_template);
}

function postmail_webformview($address,$addresscc, $subject = "", $body = "") {
  
  require_once (DRUPAL_ROOT . '/sites/all/libraries/phpmailer/class.phpmailer.php');

  $mail = new PHPMailer(); 
  //$body = eregi_replace("[\]", '', $body);
  //$body = preg_replace("^\\\^", '', $body);
  $mail->CharSet = "UTF-8"; 
  $mail->IsSMTP(); 
  $mail->SMTPDebug = 1; 
  // 1 = errors and messages
  // 2 = messages only
  
  if(variable_get('BB_ST_SMTPAuth_ST','1')){
    $mail->SMTPAuth = true; 
    $mail->SMTPSecure = variable_get('BB_ST_SMTP_SECURE','ssl');
    $mail->Host = variable_get('BB_ST_SMTP_HOST','smtp.gmail.com');
  
    $mail->Port = variable_get('BB_ST_SMTP_PORT','465');
    $mail->Username = variable_get('BB_ST_SMTP_USERNAME','xiarongze@gmail.com');
    $mail->Password = variable_get('BB_ST_SMTP_PASSWORD','xrz4253222');
  }else{
    $mail->Host = variable_get('BB_ST_SMTP_HOST','smtp.gmail.com');
  }
  
  
  $from = variable_get('BB_ST_SMTP_FROM','cdevelop@crownrelo.com');
  $from_name = variable_get('BB_ST_SMTP_FROM_NAME','CWW Development');
  $mail->SetFrom($from,$from_name);
  //$mail->AddReplyTo($from,$from_name);
  
  
  $mail->Subject = $subject;
  $mail->Body = $body;
  
  

  //if address is array
  if(is_array($address)){
    foreach($address as $addr_item){
      if($addr_item){
        $mail->AddAddress($addr_item, "");
      }
    }
  }else{
    $mail->AddAddress($address, "");
  }
  if(is_array($addresscc)){
    foreach($addresscc as $addrcc_item){
      if($addrcc_item){
        $mail->AddCC($addrcc_item, "");
      }
    }
  }else{
    $mail->AddCC($addresscc, "");
  }
  if (!$mail->Send()) {
    return false;
  }
  else {
    return true;
  }
}
